

import 'package:xsdk_flutter/xsdk/utils/json/x_utils_json.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_number.dart';

/*
双色球反向选择
1: 每组中的每个数字500万次随机,选择最多的记录组成一组, 共10组, 反选不存在的数;
2: 每组中的每个数字500万次随机,选择最少的记录组成一组, 共10组;
3: 1和2组成集合, 每次500万次随机获得最多的下标值,按下标删除直至剩余为1组数;

---x_red:[6,10,29,30,33,35], x_blue:[1]

 */
abstract class _SDoubleColorBallUtils{

  static void run(){
    //筛选出不存在和最少的
    List<int> x_red=[];
    List<int> x_blue=[];

    //处理最多
    List<int> _allReds_more=[];
    List<int> _allBlues_more=[];
    //
    for(int i=0;i<10;i++){
      Map<String,dynamic> res=_autoSelectOneGroupData_more();
      dynamic red=res['red'];
      dynamic blue=res['blue'];
      XLogUtils.printLog('---more-${i+1}, red: ${XJsonUtils.obj2Json(red)}, blue: ${XJsonUtils.obj2Json(blue)}');
      _allReds_more.addAll(red);
      _allBlues_more.add(blue);
    }


    //
    for(int i=1;i<=36;i++){
      if(!_allReds_more.contains(i)){
        if(x_red.length==0){
          x_red.add(i);
        }else{
          int index=_getIndex(length: x_red.length+1);
          x_red.insert(index, i);
        }

      }
    }
    for(int i=1;i<=16;i++){
      if(!_allBlues_more.contains(i)){
        if(x_blue.length==0){
          x_blue.add(i);
        }else{
          int index=_getIndex(length: x_blue.length+1);
          x_blue.insert(index, i);
        }
      }
    }

    //处理最少
    for(int i=0;i<10;i++){
      Map<String,dynamic> res=_autoSelectOneGroupData_less();
      dynamic red=res['red'];
      dynamic blue=res['blue'];
      XLogUtils.printLog('---less-${i+1}, red: ${XJsonUtils.obj2Json(red)}, blue: ${XJsonUtils.obj2Json(blue)}');

      for(dynamic itemR in red){
        if(!x_red.contains(itemR)){
          if(x_red.length==0){
            x_red.add(itemR);
          }else{
            int index=_getIndex(length: x_red.length+1);
            x_red.insert(index, itemR);
          }

        }
      }
      if(!x_blue.contains(blue)){
        if(x_blue.length==0){
          x_blue.add(blue);
        }else{
          int index=_getIndex(length: x_blue.length+1);
          x_blue.insert(index, blue);
        }
      }

    }


    //筛选出剩余一组
    int bSize=x_blue.length;
    while(bSize>1){
      int random=_getIndex(length:bSize);
      x_blue.removeAt(random);
      bSize=x_blue.length;
      XLogUtils.printLog('---bSize:$bSize, x_blue:${XJsonUtils.obj2Json(x_blue)}');
    }
    //
    int rSize=x_red.length;
    while(rSize>6){
      int random=_getIndex(length:rSize);
      x_red.removeAt(random);
      rSize=x_red.length;
      XLogUtils.printLog('---rSize:$rSize, x_red:${XJsonUtils.obj2Json(x_red)}');
    }
    //
    x_red.sort();

    XLogUtils.printLog('---x_red:${XJsonUtils.obj2Json(x_red)}, x_blue:${XJsonUtils.obj2Json(x_blue)}');

  }


  //自动一组(最多的)
  static Map<String,dynamic> _autoSelectOneGroupData_more(){

    //1~33
    List<int> list_redBall=[];
    while(list_redBall.length<6){
      int num=_getNumCode(isRedBall: true);

      if(!list_redBall.contains(num)){
        list_redBall.add(num);
      }
    }

    //list_redBall.sort();

    //1~16
    int blueBall=_getNumCode(isRedBall: false);

    //XLogUtils.printLog('---red: ${XJsonUtils.obj2Json(list_redBall)}, blue: $blueBall');
    Map<String,dynamic> res={};
    res['red']=list_redBall;
    res['blue']=blueBall;

    return res;
  }
  //自动一组(最少的)
  static Map<String,dynamic> _autoSelectOneGroupData_less(){

    //1~33
    List<int> list_redBall=[];
    while(list_redBall.length<6){
      int num=_getNumCode(isRedBall: true,isSelectMaxCount:false);

      if(!list_redBall.contains(num)){
        list_redBall.add(num);
      }
    }

    //1~16
    int blueBall=_getNumCode(isRedBall: false,isSelectMaxCount:false);

    //XLogUtils.printLog('---red: ${XJsonUtils.obj2Json(list_redBall)}, blue: $blueBall');
    Map<String,dynamic> res={};
    res['red']=list_redBall;
    res['blue']=blueBall;

    return res;
  }



  //500w随机最多或最小的
  static int _getNumCode(
      {
        required bool isRedBall,
        bool isSelectMaxCount=true,//是否选择出现次数最多的数
      }
      ){
    //key: num, value:count
    Map<int,int> countMap={};

    for(int i=0;i<500*10000;i++){
      //red:1~33 blue: 1-16
      int temp= XNumberUtils.randomInt(isRedBall?33:16)+1;
      if(countMap.containsKey(temp)){
        countMap[temp]=countMap[temp]!+1;
      }else{
        countMap[temp]=1;
      }
    }

    if(isSelectMaxCount){
      //
      int maxCount=0;
      int maxItem=0;
      countMap.forEach((k, v){

        if(maxCount==0){
          maxCount=v;
          maxItem=k;
        }else{
          if(v>maxCount){
            maxCount=v;
            maxItem=k;
          }
        }

      });

      return maxItem;
    }else{
      //
      int minCount=0;
      int minItem=0;
      countMap.forEach((k, v){
        if(minCount==0){
          minCount=v;
          minItem=k;
        }else{
          if(v<minCount){
            minCount=v;
            minItem=k;
          }
        }
      });

      return minItem;
    }

  }

  static int _getIndex(
      {
        required int length,
      }
      ){
    //key: num, value:count
    Map<int,int> countMap={};

    for(int i=0;i<500*10000;i++){
      //0~length-1
      int temp= XNumberUtils.randomInt(length);
      if(countMap.containsKey(temp)){
        countMap[temp]=countMap[temp]!+1;
      }else{
        countMap[temp]=1;
      }
    }

    int maxCount=0;
    int maxItem=0;
    countMap.forEach((k, v){

      if(maxCount==0){
        maxCount=v;
        maxItem=k;
      }else{
        if(v>maxCount){
          maxCount=v;
          maxItem=k;
        }
      }

    });

    return maxItem;

  }

}


void main() async {

  _SDoubleColorBallUtils.run();

}